<?php

declare(strict_types=1);

namespace App\Model;


use App\Exception\MessageException;

/**
 * @property int $id
 * @property string $platform_name 第三方平台名称
 * @property string $platform_desc 第三方平台描述
 * @property int $max_amount 最大充值金额
 * @property int $os_type 客户端类型  0:全部;1:android;2:iOS
 * @property int $pay_type
 * @property int $weight
 * @property int $trade_type 1支付宝2微信 3Apple Pay
 * @property int $valid 0禁用 1启用
 * @property int $version_min
 * @property string $appid
 * @property string $mchid
 * @property int $channel_type
 * @property int $safe_type
 * @property int $buy_type 1金币充值；2vip充值
 * @property string $package 包名限制 多个,隔开
 * @property string $logo 支付方式logo
 * @property string $uncheck_logo 未选中时logo
 * @property string $show_title 显示标题
 * @property string $limit_amount 充值限额
 * @property int $limit_type 限额方式 0无 1每天 2每周 3每五分钟（测试）
 * @property string $collect_amount 当天/本周 已收款金额
 */
class ConfigPayPlatform extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'tb_config_pay_platform';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = [];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'max_amount' => 'integer', 'os_type' => 'integer', 'pay_type' => 'integer', 'weight' => 'integer', 'trade_type' => 'integer', 'valid' => 'integer', 'version_min' => 'integer', 'channel_type' => 'integer', 'safe_type' => 'integer', 'buy_type' => 'integer', 'limit_type' => 'integer'];


    public function getPayPlatform(int $os_type, int $trade_type, mixed $package, int $version_code, float $money, mixed $pay_from)
    {
        $query = self::query()->where('weight', '>', 0)->where('max_amount', '>', $money)
            ->where(function ($query) use ($trade_type) {
                $query->where('trade_type', 0)->orWhere('trade_type', $trade_type);
            })->where(function ($query) use ($os_type) {
                $query->where('os_type', 0)->orWhere('os_type', $os_type);
            })->where(function ($query) use ($money) {
                $query->where(function ($query) use ($money) {
                    $query->where('limit_type', '<>', 0)->whereRaw("collect_amount + $money <= limit_amount");
                })->orWhere(function ($query) {
                    $query->where('limit_type', 0);
                });
            });


        if ($pay_from == 'web') {
            $query->where("platform_name like '%h5%'");
        } else {
            $query->where('valid', 1)->where('version_min', '<=', $version_code)->whereRaw("find_in_set('$package',package)");
        }

        $result = $query->select(["id", "platform_name", "pay_type", "weight"])->orderByRaw("RAND()")->get()->toArray();

        if (empty($result)) {
            // Tool::JSON(\CodeData::NEED_TIPS, '',"没有可用的支付通道");
            throw new MessageException("没有可用的支付通道");
        }

        // 根据权重获取数据
        $total_score = array_sum(array_column($result, 'weight'));
        $rand_score = rand(0, $total_score);
        foreach ($result as $val) {
            $rand_score -= $val["weight"];
            if ($rand_score <= 0) {
                $platform = $val["platform_name"];
                $pay_type = $val["pay_type"];
                $pay_platform_id = $val["id"];

                return [$platform, $pay_type, $pay_platform_id];
            }
        }

    }


}
